﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Projet_IMA.Base;
using Projet_IMA.Unsafe;


namespace Projet_IMA.Environnement
{
    public unsafe class ZBuffer
    {
        private int Height;
        private int Width;
        private float* p_TabProf;

        public ZBuffer(int Width, int Height)
        {
            this.Width = Width;
            this.Height = Height;
        }

        public bool check(UnsafePoint2D* projectionPoint)
        {
            if (projectionPoint->x >= 0 && projectionPoint->x < Width && projectionPoint->y >= 0 && projectionPoint->y < Height)
            {
                int i = projectionPoint->y * Width + projectionPoint->x;

                if (projectionPoint->profondeur < p_TabProf[i])
                {
                    p_TabProf[i] = projectionPoint->profondeur;
                    return true;
                }
            }
            
            return false;
         }

        public void Dispose ()
        {
            Unsafe.UnmanagedMemory.Free(p_TabProf);
        }

        public void Init ()
        {
            int n = Width * Height;
            p_TabProf = (float*)Unsafe.UnmanagedMemory.New<float>(n);
            for (int i = 0; i < n; i++)
            {
                p_TabProf[i] = float.MaxValue;
            }

        }

        public void setDimention(int Width, int Height)
        {
            this.Width = Width;
            this.Height = Height;
        }

    }
    /*
    public unsafe class ZBuffer
    {
        private float[][] TabProf;
        private int Height;
        private int Width;

        public ZBuffer(int Width, int Height)
        {
            this.Width = Width;
            this.Height = Height;
        }

        public bool check(UnsafePoint* Point, int x, int y)
        {
            if (x >= 0 && x < Width && y >= 0 && y < Height && Point->y < TabProf[x][y])
            {
                TabProf[x][y] = Point->y;
                return true;
            }
            else
                return false;
        }

        public void Dispose()
        {
            TabProf = null;
        }

        public void Init()
        {
            TabProf = new float[Width][];

            for (int i = 0; i < TabProf.Length; i++)
            {
                TabProf[i] = new float[Height];

                for (int j = 0; j < Height; j++)
                    TabProf[i][j] = float.MaxValue;
            }
        }

        public void setDimention(int Width, int Height)
        {
            this.Width = Width;
            this.Height = Height;
        }

    }*/
}
    